package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.foxcard.UserFoxCardData;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class UserFoxCardDaoImpl extends TempDataObjectDao<UserFoxCardData> {

    public static String getUserFoxCardDataSql = "select * from `t_u_activity_fox` where `activityId`=? and `userId`=?";
    public UserFoxCardData getUserFoxCardData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserFoxCardDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserFoxCardDataSql = "update `t_u_activity_fox` set gameTimes=?,lastGameTime=?,power=?,powerRecoverTime=?,treeScore=?,treeLevel=?,treeExp=?,lotteryRound=?,guaranteeAmount=?,guaranteePoints=?,treeCards=?,unlockCards=?,equipCards=?,maxScore=?,passStages=?,guide=?,isGoldenGuarantee=?,powerAmount=?,goldenGuaranteeAmount=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserFoxCardData userFoxCardData) {
        userFoxCardData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getGameTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, userFoxCardData.getLastGameTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, userFoxCardData.getPowerRecoverTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getTreeScore()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getTreeLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getTreeExp()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getLotteryRound()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getGuaranteeAmount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardData.getGuaranteePoints())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardData.getTreeCards())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardData.getUnlockCards())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardData.getEquipCards())));
        para.put(i++, new DbParameter(Types.VARCHAR, userFoxCardData.getMaxScore().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardData.getPassStages())));
        para.put(i++, new DbParameter(Types.VARCHAR, userFoxCardData.getGuide()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userFoxCardData.isGoldenGuarantee()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getPowerAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getGoldenGuaranteeAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userFoxCardData.getUserId()));
        if (getDb().executeNoneQuery(updateUserFoxCardDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserFoxCardDataSql = "insert into `t_u_activity_fox`(activityId,userId,gameTimes,lastGameTime,power,powerRecoverTime,treeScore,treeLevel,treeExp,lotteryRound,guaranteeAmount,guaranteePoints,treeCards,unlockCards,equipCards,maxScore,passStages,guide,isGoldenGuarantee,powerAmount,goldenGuaranteeAmount) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserFoxCardData userFoxCardData) {
        userFoxCardData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userFoxCardData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getGameTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, userFoxCardData.getLastGameTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, userFoxCardData.getPowerRecoverTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getTreeScore()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getTreeLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getTreeExp()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getLotteryRound()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getGuaranteeAmount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardData.getGuaranteePoints())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardData.getTreeCards())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardData.getUnlockCards())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardData.getEquipCards())));
        para.put(i++, new DbParameter(Types.VARCHAR, userFoxCardData.getMaxScore().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userFoxCardData.getPassStages())));
        para.put(i++, new DbParameter(Types.VARCHAR, userFoxCardData.getGuide()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userFoxCardData.isGoldenGuarantee()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getPowerAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userFoxCardData.getGoldenGuaranteeAmount()));
        if (getDb().executeNoneQuery(insertUserFoxCardDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserFoxCardData parseData(ResultSet rs) throws SQLException {
        UserFoxCardData userFoxCardData = new UserFoxCardData();
        userFoxCardData.setActivityId(rs.getInt("activityId"));
        userFoxCardData.setUserId(rs.getLong("userId"));
        userFoxCardData.setGameTimes(rs.getInt("gameTimes"));
        userFoxCardData.setLastGameTime(rs.getLong("lastGameTime"));
        userFoxCardData.setPower(rs.getInt("power"));
        userFoxCardData.setPowerRecoverTime(rs.getLong("powerRecoverTime"));
        userFoxCardData.setTreeScore(rs.getInt("treeScore"));
        userFoxCardData.setTreeLevel(rs.getInt("treeLevel"));
        userFoxCardData.setTreeExp(rs.getInt("treeExp"));
        userFoxCardData.setLotteryRound(rs.getInt("lotteryRound"));
        userFoxCardData.setGuaranteeAmount(rs.getInt("guaranteeAmount"));
        userFoxCardData.setGuaranteePoints(JSON.parseObject(rs.getString("guaranteePoints"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        userFoxCardData.setTreeCards(JSON.parseObject(rs.getString("treeCards"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.foxcard.FoxTreeCard>>() {}));
        userFoxCardData.setUnlockCards(JSON.parseObject(rs.getString("unlockCards"), Set.class));
        userFoxCardData.setEquipCards(JSON.parseObject(rs.getString("equipCards"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        userFoxCardData.setMaxScore(new BigInteger(rs.getString("maxScore")));
        userFoxCardData.setPassStages(JSON.parseObject(rs.getString("passStages"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        userFoxCardData.setGuide(rs.getString("guide"));
        userFoxCardData.setGoldenGuarantee(rs.getBoolean("isGoldenGuarantee"));
        userFoxCardData.setPowerAmount(rs.getInt("powerAmount"));
        userFoxCardData.setGoldenGuaranteeAmount(rs.getInt("goldenGuaranteeAmount"));

        return userFoxCardData;
    }
}
